package com.brainshare.an.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.brainshare.an.domain.AnWechatUser;
import com.brainshare.an.domain.LoginLog;
import com.brainshare.an.mapper.LoginLogMapper;
import com.brainshare.an.service.ILoginLogService;
import com.brainshare.common.utils.DateUtils;
import com.brainshare.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 登录日志Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-20
 */
@Service
public class LoginLogServiceImpl implements ILoginLogService
{
    @Autowired
    private LoginLogMapper loginLogMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询登录日志
     * 
     * @param id 登录日志主键
     * @return 登录日志
     */
    @Override
    public LoginLog selectLoginLogById(Long id)
    {
        return loginLogMapper.selectLoginLogById(id);
    }

    /**
     * 查询登录日志列表
     * 
     * @param loginLog 登录日志
     * @return 登录日志
     */
    @Override
    public List<LoginLog> selectLoginLogList(LoginLog loginLog)
    {
        return loginLogMapper.selectLoginLogList(loginLog);
    }

    /**
     * 新增登录日志
     * 
     * @param loginLog 登录日志
     * @return 结果
     */
    @Override
    public int insertLoginLog(LoginLog loginLog)
    {
        if (StringUtils.isNotEmpty(loginLog.getToken()))
        {
            String tmpData = redisTemplate.opsForValue().get("user:" + loginLog.getToken());
            JSONObject object = JSONObject.parseObject(tmpData);
            AnWechatUser anWechatUser = object.toJavaObject(AnWechatUser.class);
            if (anWechatUser != null)
            {
                loginLog.setWechatUserId(anWechatUser.getWechatUserId());
                loginLog.setUserName(anWechatUser.getUserName());
            }
        }
        loginLog.setCreateTime(DateUtils.getNowDate());
        return loginLogMapper.insertLoginLog(loginLog);
    }

    /**
     * 修改登录日志
     * 
     * @param loginLog 登录日志
     * @return 结果
     */
    @Override
    public int updateLoginLog(LoginLog loginLog)
    {
        return loginLogMapper.updateLoginLog(loginLog);
    }

    /**
     * 批量删除登录日志
     * 
     * @param ids 需要删除的登录日志主键
     * @return 结果
     */
    @Override
    public int deleteLoginLogByIds(Long[] ids)
    {
        return loginLogMapper.deleteLoginLogByIds(ids);
    }

    /**
     * 删除登录日志信息
     * 
     * @param id 登录日志主键
     * @return 结果
     */
    @Override
    public int deleteLoginLogById(Long id)
    {
        return loginLogMapper.deleteLoginLogById(id);
    }
}
